package com.apple1.blogservices.impl;

import com.apple1.blogdao.ArticleMapper;
import com.apple1.blogdao.LibNameMapper;
import com.apple1.blogdao.RepositoryMapper;
import com.apple1.blogentity.*;
import com.apple1.blogentity.vo.ArticleVo;
import com.apple1.blogentity.vo.LibNameVo;
import com.apple1.blogentity.vo.RepositoryVo;
import com.apple1.blogservices.RepositoryService;
import com.apple1.commonutil.BeanMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author chencheng
 */

@Service
public class RepositoryServiceImpl implements RepositoryService {


    @Autowired
    RepositoryMapper repositoryMapper;
    @Autowired
    LibNameMapper libNameMapper;
    @Autowired
    ArticleMapper articleMapper;

    /**
     * 方法名: 查询库的包含的所有文章
     * 处理逻辑:
     * note(注意): 没有查询文章所对应的标签，自己用到时可以在查
     * @param repository_id
     * @return java.util.List<com.apple1.blogentity.vo.ArticleVo>
     */
    @Override
    public List<ArticleVo> selectAllLibArticles(Integer repository_id) {

        RepositoryExample repositoryExample = new RepositoryExample();
        RepositoryExample.Criteria criteria = repositoryExample.createCriteria();

        criteria.andLibraryIdEqualTo(repository_id);

        List<Repository> repositories = repositoryMapper.selectByExample(repositoryExample);
        if(repositories == null || repositories.isEmpty() ){
            return null;
        }

        List<ArticleVo> list = new ArrayList<>();

        for(Repository repository : repositories){
            Integer articleId = repository.getArticleId();

            ArticleExample articleExample = new ArticleExample();
            ArticleExample.Criteria criteria1 = articleExample.createCriteria();
            criteria1.andTableIdEqualTo(articleId);

            List<Article> articles = articleMapper.selectByExample(articleExample);
            if (articles == null || articles.size() == 0 ){
                continue;
            }
            Article article = articles.get(0);
            ArticleVo articleVo = BeanMapUtil.getMapBean(article,ArticleVo.class);
            list.add(articleVo);

        }
        return list;
    }
    /**
     * 方法名: 删除知识库中的某个文章
     * 处理逻辑:
     * note(注意):
     * @param lib_id
     * @param article_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int delLibArtcile(Integer lib_id, Integer article_id) {
        if (lib_id == null || article_id == null){
            return 0;
        }
        RepositoryExample repositoryExample = new RepositoryExample();
        RepositoryExample.Criteria criteria = repositoryExample.createCriteria();
        criteria.andArticleIdEqualTo(article_id).andLibraryIdEqualTo(lib_id);

        int i = repositoryMapper.deleteByExample(repositoryExample);
        return i;
    }

    /**
     * 方法名: 添加文章到知识库中
     * 处理逻辑:
     * note(注意):
     * @param repositoryVo
     * @return int
     */
    @Override
    public int addArticleToRepository(RepositoryVo repositoryVo) {
        if (repositoryVo == null || repositoryVo.getLibraryId() == null
                || repositoryVo.getArticleId() == null || repositoryVo.getCreatorId() == null
        ){
            return 0;
        }
        Repository mapBean = BeanMapUtil.getMapBean(repositoryVo, Repository.class);
        mapBean.setTableId(null);

        repositoryMapper.insertSelective(mapBean);

        return 0;
    }

    /**
     * 方法名: 添加库本身数据
     * 处理逻辑:
     * note(注意):
     * @param libNameVo
     * @return int
     */
    @Override
    public int addLibName(LibNameVo libNameVo) {
        if (libNameVo == null || libNameVo.getLibName() == null || libNameVo.getCommunityId() ==null){
            return 0;
        }
        LibName mapBean = BeanMapUtil.getMapBean(libNameVo, LibName.class);
        mapBean.setTableId(null);


        return libNameMapper.insertSelective(mapBean);


    }

    /**
     * 方法名: 更新库本身数据
     * 处理逻辑:
     * note(注意):
     * @param libNameVo
     * @return int
     */
    @Override
    public int updateLibName(LibNameVo libNameVo) {
        if (libNameVo == null || libNameVo.getLibName() == null || libNameVo.getCommunityId() ==null){
            return 0;
        }
        LibName mapBean = BeanMapUtil.getMapBean(libNameVo, LibName.class);
        mapBean.setTableId(null);

        LibNameExample libNameExample = new LibNameExample();
        LibNameExample.Criteria criteria = libNameExample.createCriteria();
        criteria.andTableIdEqualTo(libNameVo.getTableId());

        return libNameMapper.updateByExampleSelective(mapBean,libNameExample);
    }
    /**
     * 方法名: 根据库id删除库本身
     * 处理逻辑: 1.删除库  2.删除库包含的所有文章
     * note(注意): 事务
     * @param lib_id
     * @return int 0-失败 非0-成功
     */
    @Override
    public int delLibName(Integer lib_id) {
        if (lib_id == null){
            return 0;
        }
        LibNameExample libNameExample = new LibNameExample();
        LibNameExample.Criteria criteria = libNameExample.createCriteria();
        criteria.andTableIdEqualTo(lib_id);

        int i = libNameMapper.deleteByExample(libNameExample);

        RepositoryExample repositoryExample = new RepositoryExample();
        RepositoryExample.Criteria criteria1 = repositoryExample.createCriteria();
        criteria1.andLibraryIdEqualTo(lib_id);

        return repositoryMapper.deleteByExample(repositoryExample);
    }
    /**
     * 方法名: 查询库本身的信息
     * 处理逻辑:
     * note(注意):
     * @param lib_id
     * @return com.apple1.blogentity.vo.LibNameVo
     */
    @Override
    public LibNameVo selectLibNameInfo(Integer lib_id) {
        if (lib_id == null){
            return null;
        }
        LibNameExample libNameExample = new LibNameExample();
        LibNameExample.Criteria criteria = libNameExample.createCriteria();
        criteria.andTableIdEqualTo(lib_id);

        List<LibName> libNames = libNameMapper.selectByExample(libNameExample);

        if (libNames == null || libNames.size() == 0){
            return  null;
        }
        LibName libName = libNames.get(0);
        return BeanMapUtil.getMapBean(libName,LibNameVo.class);
    }
}
